/*
 * Licensed to the anubis
 *
 * Copyright (c) 2023 .
 * All rights reserved.
 * 项目名称：anubis-demo-web
 * 项目描述：基于spring5的spring-mvc新特性
 * 版权说明：本软件属XXX有限公司所有，在未获得XXX有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.anubis.demo.web.controller;

import com.anubis.demo.pojo.bo.StockSubscription;
import com.anubis.demo.pojo.bo.StockSymbol;
import com.anubis.demo.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import static com.anubis.demo.constant.Constants.TEST_USER_EMAIL;

@Controller
@RequestMapping("/subscriptions")
public class SubscriptionController {

    @Autowired
    private SubscriptionService subscriptionService;

    /**
     * ...
     */
    @PostMapping
    public String addSubscription(@ModelAttribute(value = "stockSymbol") StockSymbol symbol) {
        String name = TEST_USER_EMAIL;
        subscriptionService.addSubscription(name, symbol.getSymbol());
        return "redirect:/subscriptions?added=" + symbol.getSymbol();
    }

    /**
     * ...
     */
    @GetMapping
    public String subscription(Model model) {
        String name = TEST_USER_EMAIL;
        List<StockSubscription> subscriptions = subscriptionService.findByEmail(name);
        model.addAttribute("email", name);
        model.addAttribute("subscriptions", subscriptions);

        return "subscription";
    }

}
